$(function() {
				var $form_div = $("#zbdailyTotal_form div");
				winResize($form_div);
				$(window).resize(function(){//当调整浏览器窗口的大小时，发生 resize 事件
					winResize($form_div);
				});
				loadTable();
				
})


var $form = $('#zbdailyTotal_form');

//指定列求和
function compute_GXC(colName) {
     var rows = $('#zbdailyTotal_datagrid').datagrid('getRows');
     var total = 0;
     for (var i = 0; i < rows.length; i++) {
    	 if(typeof(rows[i][colName]) != 'undefined'){
    		 total += parseFloat(rows[i][colName]);
    	 }
     }
     if(total > 0){
    	 return total.toFixed(1);
     }else{
    	 return 0;
     }
}


function  loadTable()
{	
	var columnsAll = new Array();
	columnsAll.push({field:'ids',title:'ids',align:'center',rowspan:2,hidden:true});
	
	columnsAll.push({field:'cjrs',title:'新增<br/>人数',align:'center',rowspan:2});
	columnsAll.push({field:'lc001salnum',title:'疗程数',align:'center',rowspan:2});
	columnsAll.push({field:'fwcs',title:'服务<br/>次数',rowspan:2,align:'center'});
	columnsAll.push({field:'dcrs',title:'掉秤<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'dcjs',title:'掉秤<br/>斤数',rowspan:2,align:'center'});
	columnsAll.push({field:'zcrs',title:'涨称<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'zcjs',title:'涨称<br/>斤数',rowspan:2,align:'center'});
	columnsAll.push({field:'pcrs',title:'平称<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'zjz',title:'总减重',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			var dcjs = parseFloat(0);
			var zcjs = parseFloat(0);
			if(row.dcjs!=null && row.dcjs!=""){
				dcjs = parseFloat(row.dcjs);
			}
			
			if(row.zcjs!=null && row.zcjs!=""){
				zcjs = parseFloat(row.zcjs);
			}
				
				return   parseFloat(dcjs-zcjs).toFixed(1);
		}
		
	});

	columnsAll.push({field:'sycprs',title:'精护<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'ggrs',title:'巩固<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'dbrs',title:'达标<br/>人数',rowspan:2,align:'center'});
	columnsAll.push({field:'rjdccc',title:'人均<br/>掉秤',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){
				return value;
			}else{
				var zjz = parseFloat(0);
				var dcjs = parseFloat(0);
				var zcjs = parseFloat(0);
				if(row.dcjs!=null && row.dcjs!=""){
					dcjs = parseFloat(row.dcjs);
				}
				
				if(row.zcjs!=null && row.zcjs!=""){
					zcjs = parseFloat(row.zcjs);
				}
					
				zjz =  parseFloat(dcjs-zcjs).toFixed(1);
			
				
				var ggrs = parseFloat(0);
				var sddrs = parseFloat(0);
				
				if(row.zjz!=null && row.zjz!=""){
					zjz = parseFloat(row.zjz);
				}
				
				if(row.ggrs!=null && row.ggrs!=""){
					ggrs = parseFloat(row.ggrs);
				}
				
				if(row.fwcs!=null && row.fwcs!=""){
					sddrs = parseFloat(row.fwcs);
				}
				var rjdc =0;
				if((sddrs-ggrs)!=0){
					rjdc = zjz/(sddrs-ggrs);
					
				}
				
					return  rjdc.toFixed(2);
			}
			
		}
	});
	columnsAll.push({field:'dcrszb',title:'掉秤<br/>占比',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){ 
				return  value+"%";
			}else{
				var dcrs = parseFloat(row.dcrs);
				var ggrs = parseFloat(row.ggrs);
				var dcbl = 0;
				var fwcs = parseFloat(row.fwcs);
				if((fwcs-ggrs)!=0){
					dcbl = parseFloat(dcrs/(fwcs-ggrs)*100);
					dcbl = dcbl.toFixed(2);
					dcbl = dcbl+'%';
				}
				return dcbl;
			}
		}
	});
	columnsAll.push({field:'jhzb',title:'精护<br/>占比',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){
				return value;
			}else{
				
				var sycprs =   parseFloat(0);
				var sddrs = parseFloat(0);
				if(row.fwcs!=null && row.fwcs!=""){
					sddrs = parseFloat(row.fwcs);
				}
				if(row.sycprs!=null && row.sycprs!=""){
					sycprs = parseFloat(row.sycprs);
				}
				if(sddrs!=0){
				
					var value = parseFloat(sycprs)/sddrs*100;
					value = value.toFixed(2)+"%";
				}
				
				return  value;
			}
				
			
		}
	});
	columnsAll.push({field:'jzzxse',title:'减重<br/>销售',rowspan:2,align:'center'});
	columnsAll.push({field:'cpxs',title:'产品<br/>销售',colspan:10,align:'center'});
	
	columnsAll.push({field:'pricetotal',title:'总销售',rowspan:2,align:'center'});
	
	
	
	
	
	
	var columnsAll1 = new Array();
	
	
	columnsAll1.push({field:'hf001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf003salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf004salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf005salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf011salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'jyg',title:'精油、膏',width:50,align:'center',
		formatter: function(value,row,index)
		{	
			var jyg = parseFloat(row.ss001salprice)+parseFloat(row.ss002salprice);
			return  jyg.toFixed(2);
		}
	});
	columnsAll1.push({field:'qt',title:'其他产品',width:50,align:'center',
		formatter: function(value,row,index)
		{	
			var qt = parseFloat(row.hf001salprice)+parseFloat(row.hf002salprice)
			+parseFloat(row.hf003salprice)
			+parseFloat(row.hf004salprice)
			+parseFloat(row.hf005salprice)
			+parseFloat(row.hf011salprice);
			return  qt.toFixed(2);
		}
	});
	
	$('#zbdailyTotal_datagrid').datagrid({
		cls: "theme-datagrid",
		singleSelect:true,
		fitColumns:true,
		rownumbers: true,

		frozenColumns:[[
		                {field:'provincename',title:'省份',align:'center',
							formatter: function(value,row,index){
								
								var ids = row.province;
							return '<a href="#" style="color: blue;" class="button-edit" onclick="lookInfo(\'' + ids + '\')">' + value + '</a>';
						}}
		                ]],
	    columns:[columnsAll,columnsAll1],
	    url: "zbDailyTotalStatistic/getZbDailyTotal",
	    queryParams: sy.serializeObject($form),
	    onLoadSuccess:function(){
			
			 var rows = $('#zbdailyTotal_datagrid').datagrid('getRows');
			if(rows.length > 0){
				var hjddrs = compute_GXC('fwcs');
				var hjdcjs =compute_GXC('dcjs');
				var hjzcjs =compute_GXC('zcjs');
				var hjzjz =hjdcjs-compute_GXC('zcjs');
				var hjggrs = compute_GXC('ggrs');
				var hjdcrs = compute_GXC('dcrs');
				var hjsycprs = compute_GXC('sycprs');
				
				
				var hjrjdc = 0;
				var hjdczb = 0;
				
				if(hjddrs-hjggrs!= 0){
					
					hjrjdc = hjzjz/(parseFloat(hjddrs)-parseFloat(hjggrs));
					hjrjdc = hjrjdc.toFixed(2);
				
					hjdczb = hjdcrs/(parseFloat(hjddrs)-parseFloat(hjggrs))*100;
					hjdczb = hjdczb.toFixed(2);
				}
				if(hjddrs!=0){
					
					hjsycprs = hjsycprs/hjddrs*100;
					hjsycprs = hjsycprs.toFixed(2);
					hjsycprs = hjsycprs+'%'
				}
				$('#zbdailyTotal_datagrid').datagrid('insertRow', {
					index: 0,	
					row: {provincename: '合计',
					cjrs:compute_GXC('cjrs'),
					lc001salnum:compute_GXC('lc001salnum'),
					fwcs:compute_GXC('fwcs'),
					dcrs:compute_GXC('dcrs'),
					dcjs:compute_GXC('dcjs'),
					zcrs:compute_GXC('zcrs'),
					zcjs:compute_GXC('zcjs'),
					pcrs:compute_GXC('pcrs'),
					zjz:compute_GXC('zjz'),
					sycprs:compute_GXC('sycprs'),
					ggrs:compute_GXC('ggrs'),
					dbrs:compute_GXC('dbrs'),
					rjdccc:hjrjdc,
					dcrszb:hjdczb,
					jhzb:hjsycprs,
					jzzxse:compute_GXC('jzzxse'),
					hf001salnum:compute_GXC('hf001salnum'),
					hf001salprice:compute_GXC('hf001salprice'),
					hf002salnum:compute_GXC('hf002salnum'),
					hf002salprice:compute_GXC('hf002salprice'),
					hf003salnum:compute_GXC('hf003salnum'),
					hf003salprice:compute_GXC('hf003salprice'),
					hf004salnum:compute_GXC('hf004salnum'),
					hf004salprice:compute_GXC('hf004salprice'),
					hf005salnum:compute_GXC('hf005salnum'),
					hf005salprice:compute_GXC('hf005salprice'),
					hf011salnum:compute_GXC('hf011salnum'),
					hf011salprice:compute_GXC('hf011salprice'),
					
					ss001salnum:compute_GXC('ss001salnum'),
					ss001salprice:compute_GXC('ss001salprice'),
					ss002salnum:compute_GXC('ss002salnum'),
					ss002salprice:compute_GXC('ss002salprice'),
					
					
					pricetotal:compute_GXC('pricetotal')
					}
	             });
		
			} 
		}
	});
	
	
}

//查看门店报表详细信息
function lookInfo(ids){
	loadViewDialog('查看详细信息', '/shouba2.0/zbDailyTotalStatistic/tolookInfo?organids=' + ids);
}

function searchFrom(){
//	loadTable();
	$('#zbdailyTotal_datagrid').datagrid('load',sy.serializeObject($form));
}


